package com.panda.admin.interfaces.file.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panda.admin.base.FileEntity;
import com.panda.admin.interfaces.file.mapper.SystemFileMapper;
import com.panda.admin.login.dto.FileDto;
import com.panda.admin.login.entity.SystemFile;
import com.panda.admin.interfaces.file.service.SystemFileService;
import com.panda.admin.login.enums.FileTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author tucy123
* @description 针对表【system_file(系统文件表)】的数据库操作Service实现
* @createDate 2023-07-06 15:11:58
*/
@Service
public class SystemFileServiceImpl extends ServiceImpl<SystemFileMapper, SystemFile> implements SystemFileService{

    @Autowired
    SystemFileMapper systemFileMapper;

    /**
     * 上传文件
     * @param fileName
     * @param fileEntity
     * @return
     */
    @Override
    public SystemFile uploadFile(String fileName, SystemFile fileEntity) {
        //获取文件格式
        int index=fileName.lastIndexOf(".");
        String format=fileName.substring(index + 1);

        if (format.equals("jpg") || format.equals("png") || format.equals("gif")) {
            fileEntity.setType(FileTypeEnum.IMG.getValue());
        }
        if (format.equals("doc") || format.equals("docx") || format.equals("pdf") || format.equals("txt")) {
            fileEntity.setType(FileTypeEnum.DOC.getValue());
        }
        if (format.equals("xls") || format.equals("xlsx")) {
            fileEntity.setType(FileTypeEnum.TAB.getValue());
        }
        if (format.equals("mp4") || format.equals("avi") || format.equals("flv") || format.equals("mkv")) {
            fileEntity.setType(FileTypeEnum.VID.getValue());
        }
        fileEntity.setSort(Math.toIntExact(systemFileMapper.selectCount(null)));
        fileEntity.setRemark(fileName.substring(0,index));
        this.save(fileEntity);
        return fileEntity;
    }

    /**
     * 删除文件
     * @param ids
     */
    @Override
    public Boolean deleteFile(List<Long> ids) {
        return systemFileMapper.deleteFileIds(ids);
    }

    /**
     * 分页查询文件
     * @param fileDto
     * @return
     */
    @Override
    public Page<SystemFile> pageFiles(FileDto fileDto) {
        Page<SystemFile> page = new Page<>(fileDto.getPageNum(), fileDto.getPageSize());
        QueryWrapper<SystemFile> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(fileDto.getType()),"type",fileDto.getType());
        wrapper.like(ObjectUtil.isNotEmpty(fileDto.getRemark()),"remark", fileDto.getRemark());

        return systemFileMapper.selectPage(page,wrapper);
    }

    /**
     * 更换头像
     * @param fileEntity
     * @return
     */
    @Override
    public Boolean updateIcon(FileEntity fileEntity) {
        return systemFileMapper.updateIcon(fileEntity);
    }

    /**
     * 默认头像
     * @return
     */
    @Override
    public String defaultIcon() {
        List<SystemFile> files = this.lambdaQuery().eq(SystemFile::getRemark, "默认头像").orderByAsc(SystemFile::getCreateTime).list();

        return files.get(0).getFilePath();
    }
}
